--[[
 * Copyright (C) 2015 - 2018 Ricky K. Thomson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * u should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 --]]
 
local Sound = class("Sound")

-- add menu / keybind to toggle this


function Sound:ctor()
	self.enabled = true
	self.volume = 80	
	if self.enabled then
		love.audio.setVolume( self.volume/100 )
	else
		love.audio.setVolume( 0 )
	end

	--音效
	self.effects = {
		["jump"] = love.audio.newSource("res/sounds/effect/jump.ogg", "static"),
		["gem"] = love.audio.newSource("res/sounds/effect/gem.ogg", "static"),
		["hit"] = love.audio.newSource("res/sounds/effect/hit.ogg", "static"),
		["beep"] = love.audio.newSource("res/sounds/effect/beep.ogg", "static"),
		["die"] = love.audio.newSource("res/sounds/effect/die.ogg", "static"),
		["crate"] = love.audio.newSource("res/sounds/effect/crate.ogg", "static"),
		["lifeup"] = love.audio.newSource("res/sounds/effect/lifeup.ogg", "static"),
		["kill"] = love.audio.newSource("res/sounds/effect/kill.ogg", "static"),
		["checkpoint"] = love.audio.newSource("res/sounds/effect/checkpoint.ogg", "static"),
		["goal"] = love.audio.newSource("res/sounds/effect/goal.ogg", "static"),
		["spring"] = love.audio.newSource("res/sounds/effect/spring.ogg", "static"),
		["blip"] = love.audio.newSource("res/sounds/effect/blip.ogg", "static"),
		["magnet"] = love.audio.newSource("res/sounds/effect/magnet.ogg", "static"),
		["shield"] = love.audio.newSource("res/sounds/effect/shield.ogg", "static"),
		["creek"] = love.audio.newSource("res/sounds/effect/creak.ogg", "static"),
		["slice"] = love.audio.newSource("res/sounds/effect/slice.ogg", "static"),
		["bumper"] = love.audio.newSource("res/sounds/effect/bumper.ogg", "static"),
		["brick"] = love.audio.newSource("res/sounds/effect/brick.ogg", "static"),
		["start"] = love.audio.newSource("res/sounds/music/start.ogg", "static"),
		["bounce"] = love.audio.newSource("res/sounds/effect/bounce.ogg", "static"),

	}
	--音乐
	self.music = {
		[0] = nil,
		[1] = love.audio.newSource("res/sounds/music/jungle.ogg", "stream"),
		[2] = love.audio.newSource("res/sounds/music/underwater.ogg", "stream"),
		[3] = love.audio.newSource("res/sounds/music/walking.ogg", "stream"),
		[4] = love.audio.newSource("res/sounds/music/intense.ogg", "stream"),
		[5] = love.audio.newSource("res/sounds/music/busy.ogg", "stream"),
		[6] = love.audio.newSource("res/sounds/music/intro.ogg", "stream"),
		[7] = love.audio.newSource("res/sounds/music/riverside.ogg", "stream"),
		[8] = love.audio.newSource("res/sounds/music/exploration.ogg", "stream"),
		[9] = love.audio.newSource("res/sounds/music/rainbow.ogg", "stream"),
		[10] = love.audio.newSource("res/sounds/music/level_complete.ogg", "stream"),
		[11] = love.audio.newSource("res/sounds/music/fight.ogg", "stream"),
		[12] = love.audio.newSource("res/sounds/music/paradise.ogg", "stream"),
		[13] = love.audio.newSource("res/sounds/music/happy.ogg", "stream"),
		[14] = love.audio.newSource("res/sounds/music/grasslands.ogg", "stream"),
	}
	--环境音乐
	self.ambience = {
		[0] = nil,
		[1] = love.audio.newSource("res/sounds/ambient/swamp.ogg", "stream"),
		[2] = love.audio.newSource("res/sounds/ambient/stream.ogg", "stream"),
		[3] = love.audio.newSource("res/sounds/ambient/drip.ogg", "stream"),
		[4] = love.audio.newSource("res/sounds/ambient/storm.ogg", "stream")
	}

end


--切换
function Sound:toggle()
	self.enabled = not self.enabled
	if self.enabled then
		love.audio.setVolume( self.volume/100 )
	else
		love.audio.setVolume( 0 )
	end
end



--播放环境音
function Sound:playambient(id)
	if not id then 
		id = 0
	end
	self.ambient = self.ambience[id]
	self:stoplooping(self.ambience)
		
	if id ~= 0 then
		self.ambient:setLooping(true)
		self.ambient:setVolume(1)
		self.ambient:play()
	end
end


function Sound:play(fx)
	fx:stop()
	fx:play()
end

function Sound:stoplooping(type)
	for _,t in ipairs(type) do
		t:stop()
	end
end

function Sound:playEffect(name)
	-- body
	local fx = self.effects[name]
	if fx then 
		fx:stop()
		fx:play()
	end

end

function Sound:paseEffect(name)
	local fx = self.effects[name]
	if fx then 
		fx:stop()
	end
end

--播放背景音乐
function Sound:playbgm(id)
	if not id then 
		id = 1
	end
	self.bgm = self.music[id]
	self:stoplooping(self.music)
	
	if id ~= 0 then
		self.bgm:setLooping(true)
		self.bgm:setVolume(0.5)
		self.bgm:play()
	end
end

function Sound:pausebgm(id)
	if not id then 
		id = 1
	end	
	love.audio.pause(self.music[id])
end

return Sound